<?php
	/**
	 *	KLF Framework
	 *
	 *	Written by Kristian Oye <kristianoye@gmail.com> on Oct 22, 2011
	 *	Copyright (C) 2010-2011
	 */
	namespace KLF\System\Test
	{
		/**
		 *  Class KUnitTestSuite
		 *
		 *  Contains a logical group of tests.
		 *
		 *  @package KLF\System\Test
		 *  @access public
		 *  @author Kristian Oye <kristianoye@gmail.com>
		 *  @since 0.2
		 */
		use KLF\System\Errors\KArgumentException;

		class KUnitTestSuite extends KUnitTestCase
		{
			protected $_TestCases;
			protected $_Successes;
			
			public function __construct()
			{
				$this->_TestCases = array();
			}
			
			/**
			 * Adds a test to the suite
			 *
			 * @author Kristian Oye <kristianoye@gmail.com>
			 * @since 0.2
			 * @return KUnitTestSuite Returns a reference to this suite
			*/
			public function AddTest($testName, KUnitTestCase $test)
			{
				if (!isset($this->_TestCases[$testName]))
				{
					$this->_TestCases[$testName] = $test;
				}
				else
				{
					throw new \KLF\System\Errors\KArgumentException("Test '$testName' is already defined in suite.");
				}
			}
			
			/**
			 * Fetches a test case from the suite by name.
			 * @param string $testCaseName
			 * @return KUnitTestCase
			 * @throws KArgumentException
			 */
			public function GetTest($testCaseName)
			{
				if (isset($this->_TestCases[$testCaseName]))
				{
					return $this->_TestCases[$testCaseName];
				}
				else 
				{
					throw new \KLF\System\Errors\KArgumentException("No such case '$testCaseName' in suite.");
				}
			}
			
			/**
			 * Runs the tests in the test suite.
			 * 
			 * @see KLF\System\Test.KUnitTestCase::Run()
			 */
			public function Run()
			{
				try
				{
					$this->Register();
					
					foreach($this->_TestCases as $testCaseName => $testCase)
					{
						$testCase = $this->GetTest($testCaseName);
						
						try
						{
							$testCase->Register();
							
							if ($testCase->RunTest())
								$this->_Successes ++;
							
							$testCase->UnRegister();
						}
						catch(\Exception $ex)
						{
							print "ERROR: ".$ex->getMessage();
						}
					}
				
					$this->UnRegister();
				}
				catch(\Exception $ex2)
				{
					
				}
			}
		}
	}